#include "customlineedit.h"


CustomLineEdit::CustomLineEdit(QWidget *parent)
    : QLineEdit(parent)
{
    setAlignment(Qt::AlignHCenter);
    setStyleSheet("QLineEdit::placeholder { text-align: center; }");
}

void CustomLineEdit::keyPressEvent(QKeyEvent *event)
{
    int midLength = (maxLength() + 1) / 2;
    if (event->key() == Qt::Key_Backspace)
    {
        if (text().isEmpty()) {
            // 如果删除到最后，将光标移回中间
            setCursorPosition(midLength);
        }
        else
        {
            // 否则，光标向右移动
            setCursorPosition(cursorPosition());
        }
    }
    QLineEdit::keyPressEvent(event);
}


